﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using SCSService;

namespace SCS.Core.Models
{
    public class ConfigModel
    {
        #region "Website"
        public static List<System_Website> GetListSiteByUserAdmin(int userID)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListWebsiteByUser(userID, 2);
        }

        public static System_Website GetWebsitebyDomain(string domain)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetWebsiteByDomain(domain);
        }

        public static int CreateWebshop(System_Website shop)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.CreateWebshop(shop);
        }
        #endregion
                     
        #region "Modul"
        public static List<Config_Modul> GetListModulBySiteGuid(int siteGuid,int typeModul)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListModulBySiteGuid(siteGuid, typeModul);
        }

        public static List<Config_Modul> GetListAllModulBySiteGuid(int siteGuid)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListAllModulBySiteGuid(siteGuid);
        }

        public static List<Config_Page> GetListParentPageInModul(int modulId)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListPageParentInModul(modulId);
        }

        //public static HtmlTextWriter GetListModulBuilder(int id,string uri)
        //{
        //    HtmlTextWriter sb=new HtmlTextWriter(new System.IO.StringWriter());
        //    return ListModulBuilder(ref sb, id, true, uri);
        //}

        //public static HtmlTextWriter ListModulBuilder(ref HtmlTextWriter sb, int id, bool root,string uri)
        //{
        //    Uri nuri = new Uri(uri);
        //    var arr = nuri.PathAndQuery.Split('/');

        //    List<SCSService.Config_Modul> listModul = Models.ConfigModel.GetListModulBySiteGuid(id,1);
        //    if (root)
        //    {
        //        sb.WriteBeginTag("ul");
        //        sb.WriteAttribute("id","navbar");
        //        sb.Write(">");
        //    }
        //    else
        //    {
        //        sb.WriteBeginTag("ul");
        //        sb.Write(">");
        //    }
        //    sb.Write("\n");
        //    foreach (var item in listModul)
        //    {
                
        //        sb.WriteBeginTag("li");
        //        if (root)
        //        {
        //            if (uri.Contains("/" + item.ModulName + "/"))
        //            {
        //                sb.WriteAttribute("class", "active");
        //            }
        //        }
        //        sb.Write(">");
        //        sb.WriteBeginTag("a");
        //        sb.WriteAttribute("href", "/"+arr[1]+"/"+arr[2]+"/" + item.ModulName + "/Index");            
        //        sb.Write(">");
        //        sb.Write(item.ModuleText);
        //        sb.WriteEndTag("a");              
        //        List<SCSService.Config_Page> listParentPage = Models.ConfigModel.GetListParentPageInModul(item.ModulID);
        //        if (listParentPage.Count() > 0)
        //        {
        //            sb.WriteBeginTag("ul");
        //            if (uri.Contains("/" + item.ModulName + "/"))
        //            {
        //                sb.WriteAttribute("style", "display:");
        //            }
        //            else
        //            {
        //                sb.WriteAttribute("style", "display:none");
        //            }
        //            sb.Write(">");
        //            sb.Write("\n");
        //            foreach (var page in listParentPage)
        //            {
        //                sb.WriteBeginTag("li");
        //                if (uri.Contains("/" + page.PageName))
        //                {
        //                    sb.WriteAttribute("class", "active");
        //                }                      
        //                sb.Write(">");
        //                sb.WriteBeginTag("a");
        //                sb.WriteAttribute("href", "/"+arr[1]+"/"+arr[2]+"/" + item.ModulName + "/" + page.PageName);
        //                sb.WriteAttribute("class", "subbutton white");
        //                sb.Write(">");
        //                sb.Write(page.PageText);
        //                sb.WriteEndTag("a");
        //                sb.WriteEndTag("li");

        //                sb.Write("\n");
        //            }
        //            sb.WriteEndTag("ul");
        //            sb.Write("\n");
                    
        //        }
        //        sb.WriteEndTag("li");
        //        sb.Write("\n");
        //    }
        //    sb.WriteEndTag("ul");

        //    return sb;
        //}

        public static List<Config_Modul> GetListModulforUserLogin(int siteGuid, int userID, int typeModul)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListModulforUserLogin(siteGuid, userID, typeModul);
        }

        public static List<Config_Page> GetListPageforUserLogin(int siteGuid, int userID)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListPageforUserLogin(siteGuid, userID);
        }

        public static List<Config_Page> GetListPageParentInModulforUserLogin(int modulID, int siteGuid, int userID)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListPageParentInModulforUserLogin(modulID, siteGuid, userID);
        }

        public static HtmlTextWriter GetListModulBuilder(int id, int userID, string uri)
        {
            HtmlTextWriter sb = new HtmlTextWriter(new System.IO.StringWriter());
            return ListModulBuilder(ref sb, id, userID, true, uri);
        }

        public static HtmlTextWriter ListModulBuilder(ref HtmlTextWriter sb, int id, int userID, bool root, string uri)
        {
            Uri nuri = new Uri(uri);
            var arr = nuri.PathAndQuery.Split('/');

            //
            List<Config_Modul> listModul = GetListModulforUserLogin(id, userID, 1);
            //List<SCSService.Config_Modul> listModul = Models.ConfigModel.GetListModulBySiteGuid(id,1);
            if (root)
            {
                sb.WriteBeginTag("ul");
                sb.WriteAttribute("id", "navbar");
                sb.Write(">");
            }
            else
            {
                sb.WriteBeginTag("ul");
                sb.Write(">");
            }
            sb.Write("\n");
            foreach (var item in listModul)
            {

                sb.WriteBeginTag("li");
                if (root)
                {
                    if (uri.Contains("/" + item.ModulName + "/"))
                    {
                        sb.WriteAttribute("class", "active");
                    }
                }
                sb.Write(">");
                sb.WriteBeginTag("a");
                if (item.ModulName == "Dashboard")
                {
                    sb.WriteAttribute("href", "/" + arr[1] + "/" + arr[2] + "/" + item.ModulName + "/DashboardIndex");
                }
                else
                {
                    sb.WriteAttribute("href", "/" + arr[1] + "/" + arr[2] + "/" + item.ModulName + "/Index");
                }
                sb.Write(">");
                sb.Write(item.ModuleText);
                sb.WriteEndTag("a");
                List<SCSService.Config_Page> listParentPage = Models.ConfigModel.GetListPageParentInModulforUserLogin(item.ModulID, id, userID);
                //List<SCSService.Config_Page> listParentPage = Models.ConfigModel.GetListParentPageInModul(item.ModulID);
                if (listParentPage.Count() > 0)
                {
                    sb.WriteBeginTag("ul");
                    if (uri.Contains("/" + item.ModulName + "/"))
                    {
                        sb.WriteAttribute("style", "display:");
                    }
                    else
                    {
                        sb.WriteAttribute("style", "display:none");
                    }
                    sb.Write(">");
                    sb.Write("\n");
                    foreach (var page in listParentPage)
                    {
                        sb.WriteBeginTag("li");
                        if (uri.Contains("/" + page.PageName))
                        {
                            sb.WriteAttribute("class", "active");
                        }
                        sb.Write(">");
                        sb.WriteBeginTag("a");
                        sb.WriteAttribute("href", "/" + arr[1] + "/" + arr[2] + "/" + item.ModulName + "/" + page.PageName);
                        sb.WriteAttribute("class", "subbutton white");
                        sb.Write(">");
                        sb.Write(page.PageText);
                        sb.WriteEndTag("a");
                        sb.WriteEndTag("li");

                        sb.Write("\n");
                    }
                    sb.WriteEndTag("ul");
                    sb.Write("\n");

                }
                sb.WriteEndTag("li");
                sb.Write("\n");
            }
            sb.WriteEndTag("ul");

            return sb;
        }

        public static int GetModuleIDBySiteGuidAndModuleName(int id, string name)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetModuleIDbySiteAndNameModule(id, name);
        }

        public static int CreateSystemWebsiteModule(System_Website_Modul swm)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.CreateSystemWebsiteModule(swm);
        }

        public static SCSService.Config_Modul GetModuleByID(int id)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetModuleByID(id);
        }
        public static List<Config_Modul> GetAllModule()
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetAllModule();
        }
        #endregion

        #region "MasterPage"
        public static Config_MasterPage GetMasterPageByPageID(int id,int siteGuid)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetMasterPageByPageID(id, siteGuid);
        }

        public static int CreateMasterPage(Config_MasterPage master)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.CreateMasterPage(master);
        }

        public static int CreateConfigPageMasterPage(Config_Page_MasterPage cpm)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.CreateConfigPageMasterPage(cpm);
        }
        #endregion

        #region "Page And Control"
        public static int GetPageIDByModuleIDAndPageName(int id, string name)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetPageIdbyModuleAndNamePage(id, name);
        }

        public static List<Config_Control> GetListConfigControlInPage(int id,int siteGuid)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListControlInPage(id, siteGuid);
        }

        public static bool CheckViewPage(int siteguid, int pageid)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.CheckViewPage(siteguid, pageid);
        }

        //public static List<Article_Static> GetListArticleStaticByListControlID(List<Config_Control> listControl, int LanguagesID, int siteGuid)
        //{
        //    SCSService.ConfigService configSvc = new ConfigService();
        //    return configSvc.GetListArticleStaticByListControlID(listControl, LanguagesID, siteGuid);
        //}

        //public static Config GetConfigByArticleStaticID(int id)
        //{
        //    SCSService.ConfigService configSvc = new ConfigService();
        //    return configSvc.GetConfigByArticleStaticID(id);
        //}

        public static Config_Page GetPageByID(int? id)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetPagebyId(id);
        }

        public static List<Config_Page> GetListPageInWebsite(int siteguid)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListPageInWebsite(siteguid);
        }

        public static List<Config_Page> GetListAllPageInWebsite(int siteguid)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListAllPageInWebsite(siteguid);
        }

        public static int CreateConfigPageWebsite(Config_Page_Website cpw)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.CreateConfigPageWebsite(cpw);
        }

        public static List<Config_Page_Control> GetListControlInSiteGuid(int siteGuid)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListControlInSiteGuid(siteGuid);
        }

        public static int CreateConfigPageControl(Config_Page_Control cpc)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.CreateConfigPageControl(cpc);
        }

        public static List<SCSService.Config_Page> GetListPageByParentIDAndModuleID(int? parentID, int? moduleID)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListPageByParentIDAndModuleID(parentID,moduleID);
        }
        #endregion

        #region "Main Menu Website"
        public static List<Menu> GetListMenuByParent(int? menuID, int LanguageID, int siteGuid,int status)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListMenuByParent(menuID, LanguageID, siteGuid,status);
        }

        public static Menu GetMenuByID(int? id)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetMenuByID(id);
        }

        public static Menu GetMenuBySiteGuidAndLanguageIDAndUrl(int LanguagesID, int siteGuid, string url)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetMenuBySiteGuidAndLanguageIDAndUrl(LanguagesID,siteGuid,url);
        }

        public static List<Menu> GetListMenuBySiteGuid(int siteGuid, int? status)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListMenuBySiteGuid(siteGuid, status);
        }

        public static List<Menu> GetListMenuByParentIDSearch(int? parentID, int languageID, int siteGuid, string MenuName, string MenuText, string MenuUrl, int? MenuStatus, int? SortOrder, int? TypeMenuID)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListMenuByParentIDSearch(parentID, languageID, siteGuid, MenuName, MenuText, MenuUrl, MenuStatus, SortOrder, TypeMenuID);
        }

        public static int GetCountMenu(int LanguagesID, int siteGuid)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetCountMenu(LanguagesID, siteGuid);
        }

        public static int CreateMenu(Menu menu)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.CreateMenu(menu);
        }

        public static void UpdateMenu(Menu menu)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            configSvc.UpdateMenu(menu);
        }

        public static void UpdateStatusMenu(int? id, byte? newStatus)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            configSvc.UpdateStatusMenu(id, newStatus);
        }

        public static void UpdateSortOrderMenu(int? id, int? newSort)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            configSvc.UpdateSortOrderOfMenu(id, newSort);
        }

        public static void DeleteMenu(int id)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            configSvc.DeleteMenu(id);
        }
        #endregion

        #region "Language for Website"
        public static System_Language_Tag GetTabBySiteGuidAndLanguageIDandTagName(int LanguageID, int SiteGuid, string TagName)
        { 
            SCSService.LanguageService langsvc=new LanguageService();
            return langsvc.GetTagBySiteGuidAndLanguageIDandTagName(LanguageID, SiteGuid, TagName);
        }

        public static List<System_Language_Tag> GetListTagBySiteGuid(int siteGuid)
        {
            SCSService.LanguageService langsvc = new LanguageService();
            return langsvc.GetListTagBySiteGuid(siteGuid);
        }

        public static int CreateSystemLanguageTag(System_Language_Tag tag)
        {
            SCSService.LanguageService langsvc = new LanguageService();
            return langsvc.CreateSystemLanguageTag(tag);
        }

        public static void CreateSystemWebsiteLanguage(System_Website_Language swl)
        {
            SCSService.LanguageService langsvc = new LanguageService();
            langsvc.CreateSystemWebsiteLanguage(swl);
        }
        #endregion

        #region "Metadata for page"
        public static Config_Metadata GetMetadata(int? pageId, int? menuID, string param, int? siteGuid, int? LanguageId)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetMetadataByPageIDAndMenuIdAndParam(pageId, menuID, param, siteGuid, LanguageId);
        }

        public static int CreateMetadata(Config_Metadata meta)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.CreateMetadata(meta);
        }

        public static Config_Metadata GetMetadataByID(int id)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetMetadataByID(id);
        }

        public static void UpdateMetadata(Config_Metadata meta)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            configSvc.UpdateMetadata(meta);
        }

        public static List<Config_Metadata> GetListMetadata(string TitleSearch, int? siteGuid, int langauesID)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListMetadata(TitleSearch, siteGuid, langauesID);
        }

        public static List<Config_Metadata> GetListMetadataBysiteGuid(int siteGuid)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListMetadataBysiteGuid(siteGuid);
        }

        public static void DeleteMetadata(int id)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            configSvc.DeleteMetadata(id);
        }
        #endregion
        #region Support 
        public static int CreateSupport(Support prtSupport)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.CreateSupport(prtSupport);
        }
        public static List<Support> GetListSupport(string SupportName,bool? SupportActive, int? LanguagesID, int? Sort, int? siteGuid)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            return configSvc.GetListSupport(SupportName,SupportActive,LanguagesID,Sort,siteGuid);
        }
        public static Support GetListSupportById(int id,int LanguageID,int siteGuid)
        {
             SCSService.ConfigService configSvc = new ConfigService();
             return configSvc.GetListSupportById(id, LanguageID,siteGuid);
        }
        public static void UpdateSupport(Support type)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            configSvc.UpdateSupport(type);
        }
        public static void DeleteSupport(int? typeId)
        {
            SCSService.ConfigService configSvc = new ConfigService();
            configSvc.DeleteSupport(typeId);
        }
       
        #endregion
    }
}